import numpy as np
from scipy.interpolate import interp1d
import os
import matplotlib.pyplot as plt

def preprocessing(n_MCM,S_time_sum,dead_time,uncertain_dead_time,dark_noise,uncertain_dark_noise,quantum_efficiency,
                  uncertain_quantum_efficiency,pulse_repeat,pulse_time):
    # n_MCM：MCM试验次数
    # S_time_sum：探测文件各高度L0级数据（求和后）
    # dead_time：探测器死区时间
    # uncertain_dead_time：探测器死区时间波动度
    # dark_noise：探测器暗噪声
    # uncertain_dark_noise：探测器暗噪声波动度
    # quantum_efficiency：探测器量子效率
    # uncertain_quantum_efficiency：探测器量子效率波动度
    # pulse_repeat：激光器重频
    # pulse_time：L2级积分时间

    S_time_sum = np.array(S_time_sum).reshape(-1,1).astype(np.float64)
    height_num = len(S_time_sum)
    # 光子噪声泊松分布
    S_time_sum = np.random.poisson(S_time_sum,[height_num, n_MCM])
    # S_time_sum = np.tile(S_time_sum,[1, n_MCM])
    # 非线性校正
    allSize = pulse_time * pulse_repeat    #L2数据累加的脉冲数
    S_temp = S_time_sum / allSize
    dead_time_inv = np.random.normal(dead_time*1e-9, uncertain_dead_time*0.01*dead_time*1e-9,[height_num, n_MCM])
    S_temp = S_temp / (1 - dead_time_inv * S_temp)
    S_temp = S_temp * allSize
    # 去噪
    dark_noise_inv = np.random.normal(dark_noise, uncertain_dark_noise*0.01*dark_noise,[height_num, n_MCM])
    S_Noise_inv = S_temp
    # 量子效率
    quantum_efficiency_inv = np.random.normal(1, uncertain_quantum_efficiency*0.01*quantum_efficiency*0.01,[height_num, n_MCM])
    S_Noise_inv = S_Noise_inv/quantum_efficiency_inv

    # S_Noise_inv = S_time_sum
    return S_Noise_inv

def function_Response(Response,altitude_R):
    altitude = np.arange(20,100,1)
    beta = [-155523697203.99512, -155814617538.61154, -156089097856.41034, -156353387632.64368, -156609892132.75845,
            -156862273412.87006, -157124926619.87155, -157393229730.69693, -157648502546.89307, -157904572549.81448,
            -158181296075.58005, -158488219402.35626, -158822340902.27478, -159170498233.90063, -159521321721.54706,
            -159865375668.0806, -160196235934.86288, -160517168278.7933, -160845627665.17923, -161181942178.6043,
            -161505093891.21582, -161799352666.70862, -162055233885.21555, -162277162573.5898, -162474454004.44647,
            -162632491342.12585, -162727816049.4088, -162768273084.53555, -162771307134.62683, -162726121529.63693,
            -162612255498.56546, -162434334427.50186, -162207610864.615, -161942027405.1776, -161645164121.51224,
            -161325445401.60443, -160992095414.64005, -160654640956.34625, -160320037252.55045, -159985540753.9035,
            -159651078994.55826, -159324466610.04688, -159002614103.80756, -158672258438.16452, -158344544637.1962,
            -158035187045.41998, -157713291735.48593, -157364330543.65097, -157039563836.38037, -156749470559.64545,
            -156471200494.573, -156224418522.9176, -156021891879.06863, -155839401825.97406, -155664318132.43668,
            -155493748131.7972, -155325273599.72986, -155164777908.706, -155021050893.8975, -154873770625.10022,
            -154706516683.80307, -154523204670.51892, -154303636965.798, -154022605034.29648, -153672675236.46942,
            -153262615146.3935, -152809150825.90878, -152330203014.31158, -151870378189.6516, -151457586199.62607,
            -151068013282.62994, -150728985897.39673, -150480809726.90665, -150341641216.6666, -150308474429.03778,
            -150328965168.43674, -150373632185.8087, -150484345824.85315, -150643573341.15482, -150837307444.6808]
    delta_err = [82738.60691254758, 82893.37653056394, 83039.40005963213, 83180.00222058792, 83316.46261464887, 83450.72945566803,
                 83590.46096179269, 83733.19821675161, 83869.00335496764, 84005.23259652179, 84152.44951222888, 84315.7327220736,
                 84493.48536003019, 84678.70506045474, 84865.34315588245, 85048.37985543802, 85224.3975173659, 85395.13352433663,
                 85569.87391789372, 85748.79323903564, 85920.70995014468, 86077.25561870489, 86213.38442695214, 86331.45048916714,
                 86436.40953038268, 86520.48539402799, 86571.1981383024, 86592.72128098992, 86594.33539563698, 86570.29665378197,
                 86509.71992525383, 86415.06591545384, 86294.4489799925, 86153.15857957209, 85995.22731266254, 85825.13695367625,
                 85647.79476060394, 85468.2689887947, 85290.25981837556, 85112.30768109568, 84934.37402512423, 84760.61623656438,
                 84589.39070324539, 84413.64148912346, 84239.29774700856, 84074.71950818384, 83903.47120329912, 83717.82384924332,
                 83545.04796097544, 83390.7183377527, 83242.67866313431, 83111.39065421383, 83003.64647968617, 82906.56177144006,
                 82813.4172464783, 82722.67400613826, 82633.04555507851, 82547.66184745406, 82471.19907557602, 82392.8459725759,
                 82303.86687580594, 82206.34488473882, 82089.5348658275, 81940.02587826893, 81753.86322582517, 81535.71125790493,
                 81294.46823940743, 81039.668003638, 80795.04119691874, 80575.43585822592, 80368.18306638428, 80187.8204974404,
                 80055.79077473984, 79981.75312729224, 79964.10839627369, 79975.00946963395, 79998.7723228758, 80057.67197884746,
                 80142.38101751982, 80245.44756059545]
    f1 = interp1d(altitude,beta)
    f2 = interp1d(altitude,delta_err)
    beta_R = f1(altitude_R)
    delta_err_R = f2(altitude_R)
    wavelength_inv = (Response - delta_err_R) / beta_R
    return wavelength_inv

def function_inv_simplify(RT, RW):
    T_grids = np.arange(150, 350.05, 0.1)
    V_grids = np.arange(-100, 100.05, 0.1)
    filepath = os.path.join(os.path.dirname(__file__),'_function_inv_Res.csv')
    table_WT = np.loadtxt(filepath,delimiter=',')
    len_T = len(T_grids)
    len_V = len(V_grids)
    y5 = table_WT[:len_T]
    y6 = table_WT[len_T:len_T*2]
    Temperature_inv = np.full(RT.shape,np.nan)
    Velocity_inv = np.full(RW.shape,np.nan)
    for j in range(RT.shape[1]):
        for i in range(RT.shape[0]):
            ind = np.argmin((y5 - RT[i,j]) ** 2 + (y6 - RW[i,j]) ** 2)
            [T_ind, V_ind] = divmod(ind, len_T)
            Temperature_inv[i,j] = T_grids[T_ind]
            Velocity_inv[i,j] = V_grids[V_ind]
    return Temperature_inv, Velocity_inv